package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.rmi.dgc.Lease;
import java.util.Date;

@Component
@Slf4j
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService ;

    @Scheduled(cron = "15 48 10 * * *")
    public void checkLeaseAgreementStatus() {
        log.info("----------------------定时任务启动，开始检查每一个租约的状态----------------");
        leaseAgreementService.update(new LambdaUpdateWrapper<LeaseAgreement>()
                .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED)
                .in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING)
                .lt(LeaseAgreement::getLeaseEndDate,new Date())
        );

    }

}
